from multiprocessing import Process, Queue

from utils import is_divisible


def create_mutil_process_data_map(process_num, data, worker_function, **dir_name):
    """
    
    :param process_num: 进程数量
    :param data: 数据
    :param worker_function: 工作函数
    :param dir_name:
    :return: 
    """
    delt = len(data) // process_num + 1
    procs = []
    for i in range(0, process_num):
        # for i in range(0, total_count):
        # 创建一个 Queue 对象，用于传递参数
        queue = Queue()
        start = delt * i
        end = delt * (i + 1)
        print((start, end))
        queue.put(data[start:end])
        # pool.apply_async(target=worker2, args=(start,end,))
        proc = Process(target=worker_function, args=(queue.get(), dir_name,))
        procs.append(proc)
        proc.start()
    # complete the processes
    for proc in procs:
        proc.join()
    ######  上面是多进程


def create_multiprocess_data_list(process_num, data, worker, class_dir_name='r'):
    """

    Args:
        class_dir_name: 登记目录
        process_num:  进程数
        data: 数据源
        worker: 业务逻辑函数

    Returns:

    """
    data_count = len(data)
    if is_divisible(data_count, process_num):
        delt = data_count // process_num
    else:
        delt = data_count // process_num + 1
    print("进程数", process_num, '数据量', data_count, '每个进程数据量', delt)
    procs = []
    for i in range(0, process_num):
        # 创建一个 Queue 对象，用于传递参数
        queue = Queue()
        start = delt * i
        end = delt * (i + 1)
        # print((start, end))
        queue.put(data[start:end])
        proc = Process(target=worker, args=(queue.get(),))
        procs.append(proc)
        proc.start()
    # complete the processes
    for proc in procs:
        proc.join()
